package com.cqc.other.juc.synchronize.demo2;


import java.util.concurrent.TimeUnit;

/**
 * @author cui
 * @date 2025/6/25 22:16
 * @description 原文链接：https://pzblog.cn/java/concurrent/concurrent-synchronized.html
 * <p>
 * synchronized加在静态方法上，锁是当前的class文件，多线程中是同一把锁，必须先执行完1个线程，再执行下一个线程。
 **/
public class SynchronizedTest {


    public static void main2(String[] args) throws InterruptedException {
        DataEntity dataEntity = new DataEntity();
        DataEntity dataEntity2 = new DataEntity();

        AThread aThread = new AThread(dataEntity);
        BThread bThread = new BThread(dataEntity2);

        aThread.start();
        bThread.start();

        TimeUnit.SECONDS.sleep(1);
        System.out.println("result: " + dataEntity.getCount());
    }

    public static void main(String[] args) throws InterruptedException {
        DataEntity dataEntity = new DataEntity();

        AThread aThread = new AThread(dataEntity);
        BThread bThread = new BThread(dataEntity);

        aThread.start();
        bThread.start();

        TimeUnit.SECONDS.sleep(1);
        System.out.println("result: " + dataEntity.getCount());
    }
}
